package com.asia5b.a5mall.middleware.logic.viewmodel.shop

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.removeAllOffGoods
import io.reactivex.functions.Consumer

class ShopAllFragmentVM :SimpleViewModel() {
   private var page = 1
   private var sumPage = 1000


    /**
     * @Note   加载商品
     * @param  filter 筛选条件
     */
    fun loadGoods(filter: HashMap<String, Any>, refresh: Int) {
        if (refresh <= 1) {
            page = 1
            sumPage = 1000
        }
        if (page > sumPage) {
            dismissLoadingBar()
            return
        }
        val observable = goodsApi.searchGoodsList(page, 10, filter)
        baseSubscribe(observable, SimpleNetParams(), Consumer { resultModel ->
            sumPage = resultModel.sumPage
            page = resultModel.page+1
//            providerView().loadGoodsResult(removeAllOffGoods(resultModel.data!!))
            if(!resultModel.data.isNullOrEmpty())
            {
                resultModel.data=removeAllOffGoods(resultModel.data!!)
            }
            loadGoodsResultLiveData.value=resultModel
        })
    }
    val loadGoodsResultLiveData= MutableLiveData<ResultModel<ArrayList<AbsGoodsModel>>>()
}